library(foreign)
immig <- read.dta("immigration_data.dta")
attach(immig)

immig1 <- subset(immig, techworker4==1&race!=4&zipcode>=1238&zipcode<=99362)
immig2 <- subset(immig, techworker4==0&race!=4&zipcode>=1238&zipcode<=99362)

plot(density(immig1$dscore2, from=0, to=1, na.rm=T), main="", xlab="Implicit Association Test (IAT) Score",ylab="Density", lty=1)
lines(density(immig2$dscore2, from=0, to=1, na.rm=T),lty=2)
legend(0,2.75,lty=c(1,2), c("High Technology Workers","Other Respondents"), cex=.8)

ks.test(immig1$dscore2, immig2$dscore2)

immig1 <- subset(immig, h1bvisas_scale<.3&race!=4&dscore2>0&zipcode>=1238&zipcode<=99362)
immig2 <- subset(immig, h1bvisas_scale>.6&race!=4&dscore2>0&zipcode>=1238&zipcode<=99362)

plot(density(immig1$dscore2, from=0, to=1, na.rm=T), xlab="Implicit Association Test (IAT) Score",ylab="Density", lty=1, main="", ylim=c(0,2.75))
lines(density(immig2$dscore2, from=0, to=1, na.rm=T),lty=2)
legend(0,2.75,lty=c(1,2), c("Oppose H-1B Visas","Support H-1B Visas"), cex=.8)

ks.test(immig1$indian_traits, immig2$indian_traits)

###################

error.bar <- function(x, y, upper, lower=upper, length=0.1,...){
if(length(x) != length(y) | length(y) !=length(lower) | length(lower) != length(upper))
stop("vectors must be same length")
arrows(x,y+upper, x, y-lower, angle=90, code=3, length=length, ...)
}

# Original with Error Bars (a)
rawdata1 <- matrix(c(.2794118, .395, .3455189, .3419118, .307692, .4, .380137, .366883),4,2)
ee1 <- matrix(c(.0466752,.0593294,.0162849,.0161951,.0633566,.0890371,.0224689,.0238814),4,2)

barx1 <- barplot(rawdata1, beside=T, names.arg=c("Full Sample", "Cultural Threat (Below Median)"), ylim=c(.2, .6), xpd=F, col=c("gray75", "gray55", "gray36", "gray12"),cex.names=.85, axis.lty=1,ylab="Support for H-1B Visas", font.lab=2, cex=1.1, cex.main=1.2, cex.lab=1.2) 
legend(1,.6,pch=c(15,15,15,15), col=c("gray75", "gray55", "gray36", "gray12"),c("Tech Workers", "Other White Collar Workers", "Non-Tech/Non-White Collar Workers", "Not Employed"), cex=1.1)
error.bar(barx1,rawdata1,ee1)

# Original with Error Bars (a) - Weighted
rawdata1 <- matrix(c(.2959523,.4492649,.366107,.3787124,.2993431,.4227555,.3999246,.3956532),4,2)
ee1 <- matrix(c(.05785,.0718582,.0214272,.0247635,.0836253,.0952793,.0289972,.0356459),4,2)

barx1 <- barplot(rawdata1, beside=T, names.arg=c("Full Sample", "Cultural Threat (Below Median)"), ylim=c(.2, .615), xpd=F, col=c("gray75", "gray55", "gray36", "gray12"),cex.names=.85, axis.lty=1,ylab="Support for H-1B Visas", font.lab=2, cex=1.1, cex.main=1.2, cex.lab=1.2) 
legend(1,.615,pch=c(15,15,15,15), col=c("gray75", "gray55", "gray36", "gray12"),c("Tech Workers", "Other White Collar Workers", "Non-Tech/Non-White Collar Workers", "Not Employed"), cex=1.1)
error.bar(barx1,rawdata1,ee1)

# Everything (a)
rawdata1c3 <- matrix(c(.2794118,.395,.3455189,.3419118,.307692,.4,.380137,.366883,.38,.4285714,.3762019,.3739754,.3484848,.5,.3947811,.4404762),4,4)
ee1c3 <- matrix(c(.0466752,.0593294,.0162849,.0161951,.0633566,.0890371,.0224689,.0238814,.0678985,.0756444,.0232148,.0230886,.0551946,.0664345,.018984,.0199596),4,4)

barx1c3 <- barplot(rawdata1c3, beside=T, names.arg=c("Full Sample", "IAT (BM)", "Indian Trait (BM)", "Threatened (BM)"), ylim=c(.1, .69), xpd=F, col=c("gray75", "gray55", "gray36", "gray12"),cex.names=.85, axis.lty=1,ylab="Support for H-1B Visas", font.lab=2, cex=1.1, cex.main=1.2, cex.lab=1.2) 
legend(1,.69,pch=c(15,15,15,15), col=c("gray75", "gray55", "gray36", "gray12"),c("Tech Workers", "Other White Collar Workers", "Non-Tech/Non-White Collar Workers", "Not Employed"), cex=1.1)
error.bar(barx1c3,rawdata1c3,ee1c3)


# Original with Error Bars (b)
rawdata2 <- matrix(c(.3431373, .39, .3425708, .3602941, .375, .41, .383562, .383117),4,2)
ee2 <- matrix(c(.046906,.0502109,.0153689,.0145167,.0675202,.0778561,.022142,.0224263),4,2)

barx2 <- barplot(rawdata2, beside=T, names.arg=c("Full Sample", "Cultural Threat (Below Median)"), ylim=c(.2, .6), xpd=F, col=c("gray75", "gray55", "gray36", "gray12"),cex.names=.85, axis.lty=1,ylab="Support for Indian Immigration", font.lab=2, cex=1.1, cex.main=1.2, cex.lab=1.2) 
legend(1,.6,pch=c(15,15,15,15), col=c("gray75", "gray55", "gray36", "gray12"),c("Tech Workers", "Other White Collar Workers", "Non-Tech/Non-White Collar Workers", "Not Employed"), cex=1.1)
error.bar(barx2,rawdata2,ee2)

# Original with Error Bars (b) - Weighted
rawdata2 <- matrix(c(.3485493,.4162083,.3660642,.3703402,.3518712,.4430335,.408742,.3883348),4,2)
ee2 <- matrix(c(.0660536,.0540279,.019703, .0205288,.1029492,.0828756 ,.0274099 ,.0297181  ),4,2)

barx2 <- barplot(rawdata2, beside=T, names.arg=c("Full Sample", "Cultural Threat (Below Median)"), ylim=c(.2, .615), xpd=F, col=c("gray75", "gray55", "gray36", "gray12"),cex.names=.85, axis.lty=1,ylab="Support for Indian Immigration", font.lab=2, cex=1.1, cex.main=1.2, cex.lab=1.2) 
legend(1,.615,pch=c(15,15,15,15), col=c("gray75", "gray55", "gray36", "gray12"),c("Tech Workers", "Other White Collar Workers", "Non-Tech/Non-White Collar Workers", "Not Employed"), cex=1.1)
error.bar(barx2,rawdata2,ee2)

# Everything (b)
rawdata2c3 <- matrix(c(.3431373,.39,.3425708,.3602941,.375,.41,.383562,.383117,.43,.4553571,.40625,.4385246,.4242424,.4932432,.3964646,.4583333),4,4)
ee2c3 <- matrix(c(.046906,.0502109,.0153689,.0145167,.0675202,.0778561,.022142,.0224263,.0687908,.0639551,.0211962,.0190004,.0544909,.0523445,.0174894,.0165956),4,4)

barx2c3 <- barplot(rawdata2c3, beside=T, names.arg=c("Full Sample", "IAT (BM)", "Indian Trait (BM)", "Threatened (BM)"), ylim=c(.1, .695), xpd=F, col=c("gray75", "gray55", "gray36", "gray12"),cex.names=.85, axis.lty=1,ylab="Support for Indian Immigration", font.lab=2, cex=1.1, cex.main=1.2, cex.lab=1.2) 
legend(1,.695,pch=c(15,15,15,15), col=c("gray75", "gray55", "gray36", "gray12"),c("Tech Workers", "Other White Collar Workers", "Non-Tech/Non-White Collar Workers", "Not Employed"), cex=1.1)
error.bar(barx2c3,rawdata2c3,ee2c3)


# Original with Error Bars (c)
rawdata3 <- matrix(c(.3145, .371258, .317895, .374737),2,2)
ee3 <- matrix(c(.0153387,.0156301,.0157256,.0161371),2,2)

barx3 <- barplot(rawdata3, beside=T, names.arg=c("Full Sample", "Non-Tech Workers/Not Employed"), ylim=c(.2, .6), xpd=F, col=c("gray75", "gray12"),cex.names=.85, axis.lty=1,ylab="Support for H-1B Visas", font.lab=2, cex=1.1, cex.main=1.2, cex.lab=1.2) 
legend(1,.6,pch=c(15,15), col=c("gray75", "gray12"),c("Cultural Threat (Above Median)", "Cultural Threat (Below Median)"), cex=1.1)
error.bar(barx3,rawdata3,ee3)

# Original with Error Bars (c) - Weighted
rawdata3 <- matrix(c(.349207,.3927938,.3521341,.3985467),2,2)
ee3 <- matrix(c(.0153387,.0156301,.0157256,.0161371),2,2)

barx3 <- barplot(rawdata3, beside=T, names.arg=c("Full Sample", "Non-Tech Workers/Not Employed"), ylim=c(.2, .6), xpd=F, col=c("gray75", "gray12"),cex.names=.85, axis.lty=1,ylab="Support for H-1B Visas", font.lab=2, cex=1.1, cex.main=1.2, cex.lab=1.2) 
legend(1,.6,pch=c(15,15), col=c("gray75", "gray12"),c("Cultural Threat (Above Median)", "Cultural Threat (Below Median)"), cex=1.1)
error.bar(barx3,rawdata3,ee3)

# Original with Error Bars (c) - Weighted
rawdata3 <- matrix(c(.349207,.3927938,.3521341,.3985467),2,2)
ee3 <- matrix(c(.0227703,.0222825,.0235685,.0229624),2,2)

barx3 <- barplot(rawdata3, beside=T, names.arg=c("Full Sample", "Non-Tech Workers/Not Employed"), ylim=c(.2, .615), xpd=F, col=c("gray75", "gray12"),cex.names=.85, axis.lty=1,ylab="Support for H-1B Visas", font.lab=2, cex=1.1, cex.main=1.2, cex.lab=1.2) 
legend(1,.615,pch=c(15,15), col=c("gray75", "gray12"),c("Cultural Threat (Above Median)", "Cultural Threat (Below Median)"), cex=1.1)
error.bar(barx3,rawdata3,ee3)

# Everything (c)
rawdata3c2 <- matrix(c(.3145,.371258,.3009138,.3782178,.1955882,.4186838,.317895,.374737,.3066298,.378125,.1979814,.4223726),6,2)
ee3c2 <- matrix(c(.0153387,.0156301,.0175453,.015517,.0162472,.0131449,.0157256,.0161371,.0180937,.0159821,.0166525,.0135334),6,2)

barx3c2 <- barplot(rawdata3c2, beside=T, names.arg=c("Full Sample", "Non-Tech Workers/Not Employed"), ylim=c(.1, .69), xpd=F, col=c("gray75", "gray25","gray50", "gray12"),cex.names=.85, axis.lty=1,ylab="Support for H-1B Visas", font.lab=2, cex=1.1, cex.main=1.2, cex.lab=1.2) 
legend(1,.69,pch=c(15,15), col=c("gray75", "gray25","gray50", "gray12"),c("IAT (Above Median)", "IAT (Below Median)","Indian Trait (Above Median)", "Indian Trait (Below Median)", "Threatened (Above Median)", "Threatened (Below Median)"), cex=1.1)
error.bar(barx3c2,rawdata3c2,ee3c2)


# Original with Error Bars (d)
rawdata4 <- matrix(c(.3225, .384232, .323158, .384737),2,2)
ee4 <- matrix(c(.0133113, .0149593,.0135959 , .0153871),2,2)

barx4 <- barplot(rawdata4, beside=T, names.arg=c("Full Sample", "Non-Tech Workers/Not Employed"), ylim=c(.2, .6), xpd=F, col=c("gray75", "gray12"),cex.names=.85, axis.lty=1,ylab="Support for Indian Immigration", font.lab=2, cex=1.1, cex.main=1.2, cex.lab=1.2) 
legend(1,.6,pch=c(15,15), col=c("gray75", "gray12"),c("Cultural Threat (Above Median)", "Cultural Threat (Below Median)"), cex=1.1)
error.bar(barx4,rawdata4,ee4)

# Original with Error Bars (d) - Weighted
rawdata4 <- matrix(c(.3392062,.396397,.3389582,.3991381),2,2)
ee4 <- matrix(c(.01876 ,.0197419,.0193449,.0199825),2,2)

barx4 <- barplot(rawdata4, beside=T, names.arg=c("Full Sample", "Non-Tech Workers/Not Employed"), ylim=c(.2, .615), xpd=F, col=c("gray75", "gray12"),cex.names=.85, axis.lty=1,ylab="Support for Indian Immigration", font.lab=2, cex=1.1, cex.main=1.2, cex.lab=1.2) 
legend(1,.615,pch=c(15,15), col=c("gray75", "gray12"),c("Cultural Threat (Above Median)", "Cultural Threat (Below Median)"), cex=1.1)
error.bar(barx4,rawdata4,ee4)

# Everything (d)
rawdata4c3 <- matrix(c(.3225,.384232,.2650131,.4257426,.2029412, .4307867,.323158,.384737,.2651934,.4255208,.2034161, .4311306),6,2)
ee4c3 <- matrix(c(.0133113, .0149593,.015498,.0134855,.0155527,.0115157,.0135959 , .0153871,.015939 ,.0137802 , .0159397 ,.0118072),6,2)

barx4c3 <- barplot(rawdata4c3, beside=T, names.arg=c("Full Sample", "Non-Tech Workers/Not Employed"), ylim=c(.1, .69), xpd=F, col=c("gray75", "gray25","gray50", "gray12"),cex.names=.85, axis.lty=1,ylab="Support for Indian Immigration", font.lab=2, cex=1.1, cex.main=1.2, cex.lab=1.2) 
legend(1,.69,pch=c(15,15), col=c("gray75", "gray25","gray50", "gray12"),c("IAT (Above Median)", "IAT (Below Median)","Indian Trait (Above Median)", "Indian Trait (Below Median)", "Threatened (Above Median)", "Threatened (Below Median)"), cex=1.1)
error.bar(barx4c3,rawdata4c3,ee4c3)

###################
exper <- read.dta("/Users/ceciliamo/Desktop/ResearchWithNeil/Globalization/GlobalizationStudy/GlobalizationExperiment/globalizationdata1_101509.dta")
attach(exper)

rawdata5 <- matrix(c(.4376392, .3909186, .362766, .202381, .1860465,.0705128, .4619165, .4111239, .3892111),3,3)
ee5 <- matrix(c(.0182004,.0180142,.0188977,.0573753 ,.0651258 ,.0541227 ,.0184655 ,.0182607,.0191054 ),3,3)

barx5 <- barplot(rawdata5, beside=T, names.arg=c("Full Sample", "Culturally Threatened", "Culturally Unthreatened"), ylim=c(.0, .6), xpd=F, col=c("gray75", "gray12", "gray31"),cex.names=.85, axis.lty=1,ylab="Support for H-1B Visas", font.lab=2, cex=1.1, cex.main=1.2, cex.lab=1.2) 
legend(1,.6,pch=c(15,15), col=c("gray75", "gray12", "gray31"),c("Canadian (n=449)", "Russian (n=479)", "Indian (n=470)"), cex=1.1)

error.bar(barx5,rawdata5,ee5)